package com.sky.aspect;

import com.sky.annotation.AutoFill;
import com.sky.constant.AutoFillConstant;
import com.sky.context.BaseContext;
import com.sky.enumeration.OperationType;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.time.LocalDateTime;

/**
 * @Author 鹏少
 * @Date 2023/9/14
 * @Date 11:28
 */
@Aspect
@Component
@Slf4j
//当前类为一个切面类
public class AutoFillAspect {

    @Pointcut("@annotation(com.sky.annotation.AutoFill)")
    public void pointCut() {
    }

    @Before("pointCut()")
    public void autoFill(JoinPoint joinPoint) {

        log.info("开始进行公共字段自动填充...");

        //获取签名(返回值 方法名 参数 ) such:
        Signature signature = joinPoint.getSignature();
        log.info("签名{}", signature);
        //转换成方法签名
        MethodSignature methodSignature = (MethodSignature) signature;
        //获取方法
        Method method = methodSignature.getMethod();
        //获取方法上的指定注解
        AutoFill annotation = method.getAnnotation(AutoFill.class);
        //获取注解里面的Value属性
        OperationType operationType = annotation.value();

        //获取当前时间
        LocalDateTime now = LocalDateTime.now();
        //获取当前id
        Long currentId = BaseContext.getCurrentId();

        //获取当前连接点的参数
        Object[] args = joinPoint.getArgs();
        Object entry = args[0];

        //做判断
        if (operationType == OperationType.UPDATE) {


            //获取当前参数的相应方法
            try {

                Method setUpdateTime = entry.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                Method setUpdateUser = entry.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
                setUpdateTime.invoke(entry, now);
                setUpdateUser.invoke(entry, currentId);


            } catch (Exception e) {
                e.printStackTrace();
            }

        } else if (operationType == OperationType.INSERT) {

            try {
                Method setUpdateTime = entry.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                Method setUpdateUser = entry.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
                Method setCreateUser = entry.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_USER, Long.class);
                Method setCreateTime = entry.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);
                setUpdateTime.invoke(entry, now);
                setUpdateUser.invoke(entry, currentId);
                setCreateUser.invoke(entry, currentId);
                setCreateTime.invoke(entry, now);

            } catch (Exception e) {
                e.printStackTrace();
            }


        }


    }

}
